from rest_framework import status
from rest_framework.response import Response

from celery_tasks.main import celery_app
from celery_tasks.sms.libs.yuntongxun.sms import CCP
from verifications import constants

import logging


logger = logging.getLogger('django')
@celery_app.task(name='send_sms_code')
def send_sms_code(mobile, sms_code):
    try:
        ccp = CCP()
        # ccp.send_template_sms('18516952650', ['1234', 5], 1)
        expires = constants.SMS_CODE_REDIS_EXPIRES // 60
        result = ccp.send_template_sms(mobile, [sms_code, expires], constants.SMS_CODE_TEMP_ID)
    except Exception as e:
        logger.error("发送验证码短信[异常][ mobile: %s, message: %s ]" % (mobile, e))
        # return Response(data={'message': '第三方框架错误'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    else:
        if result == 0:
            logger.info("发送验证码短信[正常][ mobile: %s %s]" % (mobile, sms_code))
            # return Response(data={'message': '发送成功'}, status=status.HTTP_200_OK)
        else:
            logger.error("发送验证码短信[失败s][ mobile: %s %s]" % (mobile, sms_code))
            # return Response(data={'message': '发送失败'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)